﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VswService_V7.OPC.DB;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using VswService_V7.Utility;
using VswService_V7.DataBase.Tables.L2INTERFACE;

namespace VswService_V7.DataBase.Tables
{
    public partial class BUNDLE_TABLE
    {
        #region field name
        public static readonly string tableName = "BUNDLE_TABLE";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_bundleNo = "BUNDLE_NO";
        public static readonly string fd_pipeNo = "PIPE_NO";
        public static readonly string fd_cradle = "CRADLE";
        public static readonly string fd_updateTime = "UPDATE_TIME";
        #endregion


        /// <summary>
        /// insert a pipefullnumber into specified bundle,create a bundleid
        /// ID format "yyyyMMxxxxxx" use time format "yyMMddHHmmssfff" instead
        /// </summary>
        /// <param name="eventNo"></param>
        /// <param name="bundleNo"></param>
        /// <param name="pipeNo"></param>
        /// <param name="cradle"></param>
        /// <returns></returns>
        public static void InsertPipeToBundle(long eventNo, string bundleNo, string pipeFullNo, string cradle)
        {
            try
            {
                string sql = string.Format("insert into {0}" +
               "({1},{2},{3},{4},{5})" +
               "values({6},'{7}','{8}','{9}','{10}')",
                   tableName,
                   fd_eventNo, fd_bundleNo, fd_pipeNo, fd_cradle, fd_updateTime,
                   eventNo, bundleNo, pipeFullNo, cradle, DateTime.Now.ToString(Formats.GetDateFormat()));
                string sqlINTF_BUNDLE = string.Format("insert into {0}" +
               "({1},{2},{3},{4},{5})" +
               "values({6},'{7}','{8}','{9}','{10}')",
                   L2INTF_BUNDLE.tableName,
                   fd_eventNo, fd_bundleNo, fd_pipeNo, fd_cradle, fd_updateTime,
                   eventNo, bundleNo, pipeFullNo, cradle, DateTime.Now.ToString(Formats.GetDateFormat()));
                string sqlINTFEVENT = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}')",
                    L2INTF_EVENT.tableName, eventNo, L2INTF_EVENT.generateOrUpdateBundletag, DateTime.Now.ToString(Formats.GetDateFormat()), "1", "L1", "");
                List<string> list = new List<string>();
                list.Add(sql);
                list.Add(sqlINTF_BUNDLE);
                list.Add(sqlINTFEVENT);
                OracleHelper.ExecuteSqlTran(list);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("InsertPipeToBundle", ex);
                throw ex;
            }
        }
    }


    /// <summary>
    /// pipe information in bundle，for printing
    /// </summary>
    public class BundlePipeList
    {
        public BundlePipeList()
        {
            pipeNoList = new List<string>();
            batchNoList = new List<string>();
            bundleNumber = "0";
            eventNo = 0;
            currentBatchNo = "0";
            currentPipeNo = "0";
        }

        public List<string> pipeNoList;
        public List<string> batchNoList;
        public string bundleNumber;
        public long eventNo;

        public string currentBatchNo;
        public string currentPipeNo;

        /// <summary>
        /// get all pipenos in the order of joining in the current bundle
        /// </summary>
        /// <returns></returns>
        public List<string> GetBundleFullPipeNoList()
        {
            int count = Math.Min(pipeNoList.Count, batchNoList.Count);
            List<string> pipeList = new List<string>();
            for (int i = 0; i < count; i++)
            {
                string fullNumber = GetFullPipeNo(batchNoList[i], pipeNoList[i]);
                pipeList.Add(fullNumber);
            }
            return pipeList;
        }


        /// <summary>
        /// get the complete pipeno of the latest kicked pipe in the current bundle
        /// </summary>
        /// <returns></returns>
        public string GetCurrentFullPipeNo()
        {
            int pipeNum = int.Parse(currentPipeNo);
            string fullPipeNumber = currentBatchNo + pipeNum.ToString("000");
            return fullPipeNumber;
        }

        /// <summary>
        /// calculate batchno+pipeno,get the complete pipeno
        /// </summary>
        /// <param name="batchNumber"></param>
        /// <param name="pipeNumber"></param>
        /// <returns></returns>
        private string GetFullPipeNo(string batchNumber, string pipeNumber)
        {
            int pipeNum = int.Parse(pipeNumber);
            string fullPipeNumber = batchNumber + pipeNum.ToString("000");
            return fullPipeNumber;
        }

        /// <summary>
        /// clear current bundle record
        /// </summary>
        public void Clear()
        {
            pipeNoList.Clear();
            batchNoList.Clear();
            bundleNumber = "0";
            eventNo = 0;
            currentBatchNo = "0";
            currentPipeNo = "0";
        }
    }

}
